package com.sn.shop.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * @auther sn
 * @date 2018/1/24
 * @description TODO
 */
@Repository
public class RedisDao {

    @Autowired
    private StringRedisTemplate template;

    /**
     * 写入key-value到数据库
     * @param key
     * @param value
     */
    public void setKey(String key, String value) {
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key, value);
    }

    /**
     * 写入key-value到数据库
     * @param key
     * @param value
     * @param timeout   过期时间
     * @param unit  过期时间单位
     */
    public void setKey(String key, String value, long timeout, TimeUnit unit) {
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key, value, timeout, unit);
    }

    public String getValue(String key) {
        ValueOperations<String , String> ops = template.opsForValue();
        return ops.get(key);
    }
}
